<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar">
        <div class="pull-right offscreen-right" style="margin-right: 20px;line-height: 50px;">
            <el-tooltip class="item" effect="dark" content="新建字典">
                <el-button type="primary" icon="el-icon-plus"  size="mini" @click="openAdd" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="排序">
                <el-button icon="el-icon-sort"  size="mini" @click="openSort" circle></el-button>
            </el-tooltip>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table :data="tableData" style="width: 100%"  size="small" :key="tableKey" border
                  :highlight-current-row="true" row-key="id" lazy
                  :load="loadChild">
            <el-table-column label="字典名称" header-align="center" prop="name"
                             width="200" :show-overflow-tooltip="true" align="left">
            </el-table-column>


            <el-table-column label="字典代码" header-align="center" prop="code"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="启用状态" header-align="center" prop="disabled" align="center"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <i v-if="scope.row.disabled"
                       class="fa fa-circle text-danger ml5"></i>
                    <i v-if="!scope.row.disabled"
                       class="fa fa-circle text-success ml5"></i>
                </template>
            </el-table-column>

            <el-table-column label="操作" header-align="center" prop="website"
                             :show-overflow-tooltip="true" align="center" width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                                              :command="{type:'add',id:scope.row.id,name:scope.row.name}">
                                添加子级
                            </el-dropdown-item>
                            <el-dropdown-item divided
                                              :command="{type:'enable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                启用
                            </el-dropdown-item>
                            <el-dropdown-item
                                    :command="{type:'disable',id:scope.row.id,name:scope.row.name,row:scope.row}">
                                禁用
                            </el-dropdown-item>
                            <el-dropdown-item divided
                                              :command="{type:'edit',id:scope.row.id,name:scope.row.name}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.id,name:scope.row.name}">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>

    </el-row>
    <el-dialog :close-on-click-modal="false"
            title="新建字典"
            :visible.sync="addDialogVisible"
            width="50%">
        <el-form :model="formData" ref="addForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="parentId" label="上级字典" label-width="80px">
                <el-cascader v-if="!isAddFromSub"
                             style="width: 100%"
                             tabindex="1"
                             :options="menuOptions"
                             :show-all-levels="false"
                             v-trim v-model="parentMenu"
                             :fetch-suggestions="menuQuery"
                             @active-item-change="menuChange"
                             @change="menuChange2"
                             @select="menuSelect"
                             placeholder="不选择则为顶级"
                ></el-cascader>
                <el-input v-if="isAddFromSub" type="text" v-trim v-model="formData.parentName" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item prop="name" label="字典名称">
                <el-input maxlength="100" placeholder="字典名称"
                          v-trim v-model="formData.name"
                          auto-complete="off" tabindex="3" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="字典代码">
                <el-input maxlength="100" placeholder="字典代码"
                          v-trim v-model="formData.code"
                          auto-complete="off" tabindex="5" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-trim v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="修改字典"
            :visible.sync="editDialogVisible"
            width="50%">
        <el-form :model="formData" ref="editForm" :rules="formRules" size="small" label-width="80px">
            <el-form-item prop="name" label="字典名称">
                <el-input maxlength="100" placeholder="字典名称"
                          v-trim v-model="formData.name"
                          auto-complete="off" tabindex="1" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="字典代码">
                <el-input maxlength="100" placeholder="字典代码"
                          v-trim v-model="formData.code"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="disabled" label="启用状态">
                <el-switch
                        v-trim v-model="formData.disabled"
                        active-color="#ff4949"
                        inactive-color="#13ce66">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doEdit">确 定</el-button>
        </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="字典排序"
            :visible.sync="sortDialogVisible"
            width="50%">
        <el-tree
                ref="sortMenuTree"
                :data="sortMenuData"
                draggable
                :allow-drop="sortAllowDrop"
                node-key="id"
                :props="defaultProps"
        >
            <span class="custom-tree-node" slot-scope="scope">
                <span>{{ scope.node.label }}</span>
            </span>
        </el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="sortDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="doSort">确 定</el-button>
        </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editDialogVisible: false,
                sortDialogVisible: false,
                isAddFromSub: false,
                tableData: [],//后台取出的数据
                formData: {
                    id: "",
                    parentId: "",
                    parentName: "",
                    type: "menu",
                    target: "data-pjax",
                    children: "false",
                },
                formRules: {
                    name: [
                        {required: true, message: '字典名称', trigger: ['blur', 'change']}
                    ],
                    code: [
                        {required: true, message: '字典代码', trigger: ['blur', 'change']}
                    ]
                },
                menuOptions: [],
                parentMenu: [],
                sortMenuData: [],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                tableKey:""
            }
        },
        methods: {
            menuLoadTree: function (val, cb) {
                var url = base + "/platform/sys/dict/tree";
                $.post(url, {pid: val}, function (data) {
                    if (data.code == 0) {
                        cb(data.data);
                    }
                }, "json");
            },
            menuQuery: function (queryString, cb) {
                cb(this.menuOptions);
            },
            menuSelect: function (val) {
            },
            menuTree: function (table, data, pid) {//树形数据的级联查找很是头疼
                var self = this;
                table.findIndex(function (obj) {
                    if (pid == obj.value) {
                        obj.children = data;
                    } else if (obj.children && obj.children.length > 0) {
                        self.menuTree(obj.children, data, pid);
                    }
                });
            },
            menuChange2: function (val) {
                var self = this;
                if (val[0] == "root") {
                    self.parentMenu = [];
                }
            },
            menuChange: function (val) {
                var self = this;
                var pid = val[val.length - 1];
                self.parentMenu = val;
                self.menuLoadTree(pid, function (tree) {
                    self.menuTree(self.menuOptions, tree, pid);
                });
            },
            //排序树加载
            sortMenuLoad: function () {
                var self = this;
                $.post(base + "/platform/sys/dict/menuAll", {}, function (data) {
                    if (data.code == 0) {
                        self.sortMenuData = data.data;
                    }
                }, "json");
            },
            //排序树控制不可跨级拖拽
            sortAllowDrop: function (moveNode, inNode, type) {
                if (moveNode.data.parentId == inNode.data.parentId) {
                    return type == 'prev';
                }
            },
            //打开排序功能
            openSort: function () {
                this.sortDialogVisible = true;
                this.sortMenuLoad();
            },
            //递归获取所有的ID值
            getTreeIds: function (ids, data) {
                var self = this;
                data.forEach(function (obj) {
                    ids.push(obj.id);
                    if (obj.children && obj.children.length > 0) {
                        self.getTreeIds(ids, obj.children);
                    }
                });
            },
            //提交排序后的数据
            doSort: function () {
                var self = this;
                var ids = [];
                self.getTreeIds(ids, self.sortMenuData);
                $.post(base + "/platform/sys/dict/sortDo", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        self.$message({
                            message: data.msg,
                            type: 'success'
                        });
                        self.sortDialogVisible = false;
                        self.initTreeTable();
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            openAdd: function () {
                var self = this;
                self.isAddFromSub = false;
                self.addDialogVisible = true;
                self.parentMenu = [];
                //表单还原为初始值
                self.formData = {
                    id: "",
                    parentId: "",
                    parentName: "",
                    type: "menu",
                    target: "data-pjax",
                    children: "false",
                };
                if (this.$refs["addForm"])
                    this.$refs["addForm"].resetFields();
                self.menuLoadTree("", function (val) {
                    self.menuOptions = val;
                });
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/sys/dict/addDo";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        if (!self.isAddFromSub) {//如果不是添加子菜单，则从级联选择框取父节点ID
                            self.formData.parentId = self.parentMenu[self.parentMenu.length - 1] || "";
                        }
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.initTreeTable();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/sys/dict/editDo";
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.initTreeTable();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            initTreeTable: function () {
                var self = this;
                var url = base + "/platform/sys/dict/child";
                $.post(url, {pid: ""}, function (data) {
                    if (data.code == 0) {
                        self.tableData = data.data;
                        //self.tableKey = Math.random();
                    }
                }, "json");

            },
            loadChild: function (tree, treeNode, resolve) {
                //console.log(tree, treeNode, resolve)
                var url = base + "/platform/sys/dict/child";
                $.post(url, {pid: tree.id}, function (data) {
                    if (data.code == 0) {
                        resolve(data.data);
                    }
                }, "json");
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                if ("add" == command.type) {
                    self.openAdd();
                    self.formData.parentId = command.id;
                    self.formData.parentName = command.name;
                    self.isAddFromSub = true;
                }
                if ("edit" == command.type) {
                    $.post(base + "/platform/sys/dict/edit/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;//加载后台表单数据
                            self.editDialogVisible = true;//打开编辑窗口
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("enable" == command.type || "disable" == command.type) {
                    $.post(base + "/platform/sys/dict/" + command.type + "/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.$message({
                                message: data.msg,
                                type: 'success'
                            });
                            if ("disable" == command.type) {
                                command.row.disabled = true;
                            }
                            if ("enable" == command.type) {
                                command.row.disabled = false;
                            }
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.name, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/sys/dict/delete/" + command.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.initTreeTable();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            }

        },
        created: function () {
            this.initTreeTable();
        }
    });
</script>
<!--#
}
#-->
